// pages/drawCash/drawCash.js
import { $wuxToast } from '../../dist-wux/index'

var app = getApp()
var httpUtils = require('../../utils/httpUtils')
var api = require("../../utils/API")
var utils = require("../../utils/utils")
var commonFields = require("../../utils/commonFields")
const digital = (value) => /^[0-9]*$/.test(value)
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: null,
        withdrawalAmount: 0,
        realName: "",
        alipayAccount: "",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let userInfo = utils.getStorageSync(commonFields.userInfo);
        console.log("提现申请页面取出的userInfo信息", userInfo)
        this.setData({
            userInfo: userInfo,
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    onClickDrawCash: function (e) {
        console.log(e)
        let that = this;
        if (this.data.withdrawalAmount == 0) {
            that.showToastText("请输入金额")
            return
        }
        // if (this.data.alipayAccount.length === 0) {
        //     that.showToastText("请输入支付宝账号")
        //     return
        // }
        // if (this.data.realName.length === 0) {
        //     that.showToastText("请输入姓名")
        //     return
        // }
        if (!digital(this.data.withdrawalAmount)) {
            that.showToastText("金额输入错误")
            return
        }
        if (this.data.withdrawalAmount > this.data.userInfo.cash) {
            this.showToastText("您没有这么多钱可提取")
            return
        }
        httpUtils.postWithdrawApply(
            {
                cash: that.data.withdrawalAmount,
                realName: that.data.realName,
                alipayAccount: that.data.alipayAccount,
            },
            res => {
                console.log(res)
                if (res.code == 0) {
                    wx.showToast({
                        title: "申请提现成功",
                        icon: 'warn',
                        duration: 2000
                    })
                    setTimeout(() => {
                        wx.switchTab({
                            url: api.PAGES_MINE_MINE
                        })
                    }, 1500)
                }
            }
        )
    },
    onInputChange(e) {
        console.log("drawCash onChang", e)
        let id = e.currentTarget.id
        let value = utils.trim(e.detail.value)
        console.log("input id", id)
        if (id === "withdrawal-amount") {
            this.setData({
                withdrawalAmount: value,
            })
        } else if (id === "alipay-account") {
            this.setData({
                alipayAccount: value,
            })
        } else if (id === "real-name") {
            this.setData({
                realName: value,
            })
        }
    },

    showToastText(text) {
        $wuxToast().show({
            type: 'text',
            duration: 1500,
            color: '#fff',
            text: text,
            success: () => console.log(text)
        })

        // The same as above
        // $wuxToast().info('文本提示', {
        //     duration: 1500,
        //     color: '#fff',
        //     success: () => console.log('文本提示')
        // })
    },
})